/*
 * Copyright 2008-2010 GP Software
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 * InputValidator.java | Created on July 6, 2010
 */
package com.pacm.i18n.validator.impl;

import com.pacm.i18n.validator.Validator;
import com.pacm.i18n.validator.ValidatorException;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.net.URL;

/**
 * The command line input validator
 *
 * @author Paul Augustine C. Mendoza
 * @since 1.1
 */
public class InputValidator implements Validator {

    /**
     * The main message identifier
     */
    private static final String MAIN_MESSAGE = "INVALID ARGUMENTS:\n\t";

    /**
     * The dummy file identifier
     */
    private static final String DUMMY_FILE = "dummy.file";

    /**
     * The input
     */
    private String[] input;

    /**
     * The constructor with input
     *
     * @param input the input
     */
    public InputValidator(String[] input) {
        this.input = input;
    }

    /**
     * @see com.pacm.i18n.validator.Validator#validate()
     */
    public void validate() throws ValidatorException {
        StringBuffer msg = new StringBuffer(MAIN_MESSAGE);
        validateNull(msg);
        validateInputCount(msg);
        validateInputFile(msg);
        validateBundleName(msg);
    }

    /**
     * Validates a null input
     *
     * @param message the message
     * @throws ValidatorException if validation fails
     */
    private void validateNull(StringBuffer message) throws ValidatorException {
        final String errMsg = "Input cannot be null.";
        if (this.input == null) {
            message.append(errMsg);
            throw new ValidatorException(message.toString(), this);
        }
    }

    /**
     * Validates the input count
     *
     * @param message the message
     * @throws ValidatorException if validation fails
     */
    private void validateInputCount(StringBuffer message) throws
            ValidatorException {
        final String errMsg = "Usage: run <input filename> <output filename prefix>";
        if (this.input.length != 2) {
            message.append(errMsg);
            throw new ValidatorException(message.toString(), this);
        }
    }

    /**
     * Validates if the input file exists
     *
     * @param message the message
     * @throws ValidatorException if validation fails
     */
    private void validateInputFile(StringBuffer message) throws
            ValidatorException {
        final String errMsg = "Missing input file.";
        boolean hasErrors = false;
        String filename = (this.input[0] == null) ? DUMMY_FILE : this.input[0];
        URL url = this.getClass().getClassLoader().getResource(filename);
        if (url == null) {
            message.append(errMsg);
            throw new ValidatorException(message.toString(), this);
        }
    }

    /**
     * Validates the bundle name
     *
     * @param message the message
     * @throws ValidatorException if validation fails
     */
    private void validateBundleName(StringBuffer message) throws
            ValidatorException {
        final String errMsg = "Invalid bundle name.";
        if (StringUtils.isBlank(this.input[1])) {
            message.append(errMsg);
            throw new ValidatorException(message.toString(), this);
        }
    }
}
